package com.leaf.spring.y201906.day25.arithmetic.round;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.leaf.spring.y201906.day25.arithmetic.ServerIps;

/**
 * 轮询
 * 
 * @Title:
 * @Description:
 * @Author: leaf
 * @Since:2019年6月25日
 * @Version:1.1.0
 */
public class RoundRobinArith {

	private static Integer pos = 0;

	public String getServer() {

		String server = null;

		synchronized (pos) {

			if (pos >= ServerIps.LIST.size()) {
				pos = 0;
			}

			server = ServerIps.LIST.get(pos);

			System.out.println("===" + pos);
			try {
				Thread.sleep(new Random().nextInt(3000));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			pos++;
		}

		return server;
	}

	public static void main(String[] args) {

		ExecutorService threadPools = Executors.newFixedThreadPool(5);

		for (int i = 0; i < 5; i++) {
			threadPools.execute(() -> {
				for (int j = 0; j < 10; j++) {
					System.out.println(new RoundRobinArith().getServer() + "    <>   " + j);
				}
			});
		}

	}

}
